<?php 
require_once 'init.php';
require_once 'ckLogin.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
//定义表名
define("TABLENAME","cuxiao");
define("TABLENAME1","admin");
define("TABLENAME2","category");
//提交处理
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
	if ($_POST['contentID_1']){
		$picture_1 = $_POST['picture_1'];
		$title_1 = $_POST['title_1'];
		$cate_id_1 = (int)$_POST['cate_id_1'];
		$goodsUrl_1 = $_POST['goodsUrl_1'];
		$commission_1 = $_POST['commission_1'];
		$sort_1 = (int)$_POST['sort_1'];
		$saledNum_1 = (int)$_POST['saledNum_1'];
		$contentID_1 = (int)$_POST['contentID_1'];
		$onsale_1 = $_POST['onsale_1'];
		$db->sql = "UPDATE cuxiao SET picture='".$picture_1."',title='".$title_1."',goodsUrl='".$goodsUrl_1."',sort=".$sort_1.",cateID=".$cate_id_1.",saledNum=".$saledNum_1.",commission=".$commission_1.",onSale=".$onsale_1."  WHERE id=".$contentID_1;
		$db->doExecute();
 
	}
	
}
//条件
$where = "WHERE p.`status`=1 ";
if(!empty($_GET['cateid']) || !empty($_GET['on_sale'])){
	if(!empty($_GET['cateid'])) $where.= "AND c.id='".$_GET['cateid']."' ";
	if(!empty($_GET['on_sale'])){
		if ($_GET['on_sale']==1){
			$where.= "AND p.onSale=1 ";
		}elseif ($_GET['on_sale']==2){
			$where.= "AND p.onSale=0 ";
		}
	}
}
//排序publishDate Updater shared collected
if(!empty($_GET['sort'])){
	$sortarray = explode('_', $_GET['sort']);
	$order = "p.".$sortarray[0]." ".$sortarray[1].",";
}else{
	$order = '';
}
//总条数
$db->sql="SELECT count(p.id) count_id FROM ".TABLENAME." p LEFT JOIN ".TABLENAME2." c ON c.id = p.cateID ".$where;
$count = $db->doResultOne();
//分页
if(empty($_GET['page'])){$page = 1;}else{$page=$_GET['page'];}
if(empty($_GET['pagesize'])){$pagesize = PAGESIZE;}
else{$pagesize = $_GET['pagesize'];}
$pageInfo = page($count, $pagesize, $page);
$maxPage = $pageInfo['maxPage'];
$start = $pageInfo['start'];
$pages = $pageInfo['pages'];

$db->sql="SELECT p.*,c.cate FROM ".TABLENAME." p LEFT JOIN ".TABLENAME2." c ON c.id = p.cateID ".$where." ORDER BY ".$order." p.id ASC LIMIT ".$start.",".$pagesize;
$pictures = $db->doResultList2();
//分类
$db->sql="SELECT * FROM category";
$category = $db->doResultList2();
//var_dump($pictures);die();
//url
$url= "?on_sale=".$_GET['on_sale']."&pagesize=".$_GET['pagesize']."&cateid=".$_GET['cateid']."&sort=".$_GET['sort'];
$url2="?on_sale=".$_GET['on_sale']."&pagesize=".$_GET['pagesize']."&cateid=".$_GET['cateid'];

$admins = getAdmin($db);
//var_dump($admins);
//用户列表
function getAdmin($db){
	$db->sql = "SELECT * FROM admin WHERE id != 1";
	return $admins = $db->doResultList2();
}
//分页
function page($total,$size,$page){
	//最大页数
	if($total%$size==0){
		$maxPage = $total/$size;
	}else{
		$maxPage = (int)($total/$size)+1;
	}
	//开始条数
	$start = ($page-1)*$size;
	//分页
	if($maxPage>=5){
		if($page<=2){
			$pages=array(1,2,3,4,5);
		}elseif($page>=($maxPage-1)){
			$pages=array($maxPage-4,$maxPage-3,$maxPage-2,$maxPage-1,$maxPage);
		}else{
			$pages=array($page-2,$page-1,$page,$page+1,$page+2);
		}
	}else{
		for($i=1;$i<=$maxPage;$i++){
			$pages[]=$i;
		}
	}
	if($page<=1){$previous=1;}else{$previous=$page-1;} 
	if($page>=$maxPage) {$next = $maxPage;}else{ $next = $page+1;}
	return array('maxPage'=>$maxPage,'start'=>$start,'pages'=>$pages,'previous'=>$previous,'next'=>$next);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simpla Admin</title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css"
	media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css"
	media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css"
	media="screen" />
<!-- Colour Schemes
		Default colour scheme is green. Uncomment prefered stylesheet to use it.
		<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
		<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />  
		-->
<!-- Internet Explorer Fixes Stylesheet -->
<!--[if lte IE 7]>
			<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
		<![endif]-->
<!--                       Javascripts                       -->
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>

<script type="text/javascript">
$(function($) {
	$(".editbutton").click(function(){
		var tds = $(this).parent('td').siblings();

		$("#picture_1").val(tds.eq(2).find('img').attr("src"));
		$("#title_1").val(tds.eq(3).text());
		$('#cate_id_1 option:selected').attr("selected",false);
		$("#cate-option-"+tds.eq(4).children('span').text()+'_1').attr("selected",true);
		$("#goodsUrl_1").val(tds.eq(5).text());
		$("#sort_1").val(tds.eq(7).text());
		$("#commission_1").val(tds.eq(6).children('span').text());
		$("#saledNum_1").val(tds.eq(8).text());
		$("#contentID_1").val(tds.eq(1).text());

		$('#onsale_1 option:selected').attr("selected",false);
		$("#onsale-option-"+tds.eq(9).find("span").text()+'_1').attr("selected",true);
		
		$("#message_click").click();
	});
	$(".delbutton").click(function(){ 
		if(window.confirm('你确定要删除该促销商品！')){
			//alert($(this).parent().parent().children("td:eq(1)").text());
			//删除数据
			$.ajax({
				   type: "GET",
				   url: "youpinAjax.php",
				   data: "id="+$(this).parent().parent().children("td:eq(1)").text()+"&act=cuxiaoDel",
				   async: false,
				   success: function(date){
						if(date == 1) alert('删除成功！！');
						if(date == -1)alert('删除失败，请刷新后在删除！！！');
					}
			} );
			$("tbody tr:eq("+$(this).parent().parent().index()+")").remove();
		}
	});
	//多数据操作
	$(".xiugai").click(function(){
		//alert($("#caozuo option:selected").val());
			var text="";  
	        $("input[name=picSel]").each(function() {  
	            if ($(this).attr("checked")) {  
	                text += ","+$(this).val();  
	            }  
	        });
	        //alert(text);
	        window.location.replace('moreEdit.php?ids='+text);
		
	});
	//导出
	$("#download").click(function(){
		var cateid = $('#cateid').val();
		var on_sale   = $('#on_sale').val();
		window.open('downloadExcel3.php?cateid='+cateid+'&on_sale='+on_sale);
			
	});
	$(".onsaleStatus").live('click',function(){
		var val = $(this).siblings('span').text();
		var id = $(this).parent('td').siblings().eq(1).text();
		var td = $(this).parent('td');
		//修改状态
		$.ajax({
			type: "GET",
			url: "youpinAjax.php",
			data: "id="+id+"&saleVal="+val+"&act=changeSaleStatus",
			async: false,
			success: function(date){
				if(date != -1){
					if(date == 1){
						td.html('<a href="javascript:void(0);" class="onsaleStatus">下架中</a><span style="display: none;">0</span>');
					}else if(date == 0){
						td.html('<a href="javascript:void(0);" class="onsaleStatus">上架中</a><span style="display: none;">1</span>');
					}
					alert("修改成功");
				}else{
					alert('修改失败');}
				}
		});
		
	});
});

</script>

<!-- jQuery Configuration -->
<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery my97datepicker -->
<script type="text/javascript" src="resources/scripts/My97DatePicker/WdatePicker.js"></script>
<!-- jQuery colorbox -->
<link rel="stylesheet" href="resources/css/colorbox.css" />
<script src="resources/scripts/colorbox/jquery.colorbox.js"></script>
<script type="text/javascript">
$(function($) {
	$(".group-pic").colorbox({rel:'group-pic',width:"50%", height:"75%"});
});
</script>
</head>
<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p style="">
				<strong>简短描述输入及修改</strong>
			</p>
			<form action="<?php echo $url."&page=".$page;?>" method="post">
				<fieldset>
					图片：
					<input class="text-input " type="text" id="picture_1" name="picture_1" style="width: 310px;"/><br />
					标题：
					<input class="text-input " type="text" id="title_1" name="title_1" style="width: 310px;"/><br />
					分类：
					<select id="cate_id_1" name="cate_id_1" style="width: 300px;">
						<option value="" ></option>
						<?php foreach ($category as $keyY=>$itemY):?>
						<option id="cate-option-<?php echo $itemY['id'];?>_1" value="<?php echo $itemY['id'];?>" ><?php echo $itemY['cate'];?></option>
						<?php endforeach;?>
					</select><br />
					链接：
					<input class="text-input " type="text" id="goodsUrl_1" name="goodsUrl_1" style="width: 310px;"/><br />
					佣金：
					<input class="text-input " type="text" id="commission_1" name="commission_1" style="width: 45px;"/>%
					排序：
					<input class="text-input " type="text" id="sort_1" name="sort_1" value="1" style="width: 70px;"/>
					人气：
					<input class="text-input " type="text" id="saledNum_1" name="saledNum_1" value="" style="width: 70px;"/><br />
					上下架
					<select id="onsale_1" name="onsale_1" style="">
						<option id="onsale-option-1_1" value="1" >上架中</option>
						<option id="onsale-option-0_1" value="0" >下架中</option>
					</select><br />			
					<input id="contentID_1" name="contentID_1" type="hidden" value="" />
					<div style="clear: both;height: 1px;background-color: #D5D5D5;margin: 10px 0;"></div>
					
					<input class="button" type="submit" value="修改" id="taobao_item_subm"/>
					<a href="javascript:void(0);" class="close" onclick="$(this).parents('#facebox').hide();"> <img src="resources/images/closelabel.jpg" title="close" class="close_image" /> </a>
				</fieldset>
			</form>
		</div>
		<!-- End #messages -->
		<div id="main-content">
			<!-- Main Content Section with everything -->
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">促销商品列表！！！</p>
			<div class="clear"></div>
			<!-- End .clear -->
			<div class="content-box">
				<!-- Start Content Box -->
				<div class="content-box-header">
					<h3>促销<?php echo WHAT;?>列表</h3>
					<!-- <ul class="content-box-tabs">
						<li><a href="#tab1" class="default-tab">Table</a></li>
						href must be unique and match the id of target div
						<li><a href="#tab2">Forms</a></li>
					</ul> -->
					<div class="clear"></div>
				</div>
				<!-- End .content-box-header -->
				<div class="content-box-content">
					<form method="get" action="">
						分类：
						<select id="cateid" name="cateid" style="">
							<option value="" ></option>
							<?php foreach ($category as $keyY=>$itemY):?>
							<option id="cate-option-<?php echo $itemY['id'];?>" value="<?php echo $itemY['id'];?>" <?php if($_GET['cateid']==$itemY['id']) echo 'selected';?>><?php echo $itemY['cate'];?></option>
							<?php endforeach;?>
						</select>
						&nbsp;&nbsp;&nbsp;
						上下架：
						<select id="on_sale" name="on_sale" >
							<option value="" ></option>
							<option id="on_sale-option-1" value="1" <?php if (isset($_GET['on_sale'])) if ($_GET['on_sale']==1) echo 'selected';?>>上架中</option>
							<option id="on_sale-option-2" value="2" <?php if (isset($_GET['on_sale'])) if ($_GET['on_sale']==2) echo 'selected';?>>下架中</option>
						</select>
						&nbsp;&nbsp;&nbsp;
						每页<input name="pagesize" type="text" size="2" value="<?php echo $pagesize;?>"/>条
						&nbsp;&nbsp;&nbsp;
						<button class="button" id="zhixing" type="submit">搜索</button>
						<button class="button" id="download" type="submit">导出</button>
						<input type="hidden" name="page" value="<?php echo $page;?>"/>
						
					</form>
					
					<hr />
					<div class="clear"></div>
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->

						<table>
							<thead>
								<tr>
									<td colspan="11">
										<div class="bulk-actions align-left">
											
										</div>
										<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="<?php //echo $url;?>&page=1" title="First Page">&laquo; First</a>
											<a href="<?php //echo $url."&page=".$pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="<?php echo $url."&page=".$item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo $url."&page=".$pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo $url."&page=".$maxPage;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<div class="clear"></div>
									</td>
								</tr>
							</thead>
							<thead>
								<tr>
									<th><input class="check-all" type="checkbox" value="-1"/></th>
									<th>编号</th>
									<th>图片</th>
									<th style="width: 10%;">标题</th>
									<th style="">分类</th>
									<th style="width: 15%;" class="taobao_url_css">链接</th>
									<th style="width: 6%;">佣金</th>
									<th>排序</th>
									<th>人气</th>
									<th>上下架</th>
									<th style="width: 10%;">操作</th>
								</tr>
							</thead>
							<tbody>
							<?php if(count($pictures)>0):?>
							<?php foreach ($pictures as $key=>$item):?>
								<tr>
									<td><input type="checkbox" name="picSel" value="<?php echo $item['id'];?>"/></td>
									<td><?php echo $item['id'];?></td>
									<td><a href="<?php if($item['comefrom']==2) echo $item['picture'];else echo '../'.$item['picture'];?>" title="<?php echo $item['title'];?>" target="_blank">
											<img src="<?php echo $item['picture'];?>" alt="" width="50px;" height="50px;"/>
										</a></td>
									<td><?php echo $item['title'];?></td>
									<td><?php echo $item['cate'];?><span style="display: none;"><?php echo $item['cateID'];?></span></td>
									<td><?php echo $item['goodsUrl'];?></td>
									<td><?php if(!empty($item['commission'])) echo '<span>'.$item['commission'].'</span>%';?></td>
									<td><?php echo $item['sort'];?></td>
									<td><?php echo $item['saledNum'];?></td>
									<td>
									<?php if ($item['onSale']==1) :?>
										<a href="javascript:void(0);" class="onsaleStatus">上架中</a><span style="display: none;"><?php echo $item['onSale'];?></span>
									<?php else:?>
										<a href="javascript:void(0);" class="onsaleStatus">下架中</a><span style="display: none;"><?php echo $item['onSale'];?></span>
									<?php endif;?>
									</td>
									<td>
										<!-- Icons --> 
										<a href="javascript:void(0);" title="Edit" class="editbutton" id="<?php echo $item['id'];?>">修改</a> <br />
										<a href="javascript:void(0);" title="Delete" class="delbutton" id="<?php echo $item['id'];?>">删除</a> <br />
										<!-- <a href="#" title="Edit Meta"><img src="resources/images/icons/hammer_screwdriver.png" alt="Edit Meta" /></a> -->
									</td>
									
								</tr>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 30px; font-weight: bold;"><td colspan="10" >没有<?php echo WHAT;?></td></tr>
							<?php endif;?>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="11">
										<div class="bulk-actions align-left">
										</div>
										<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="<?php echo $url;?>&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo $url."&page=".$pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="<?php echo $url."&page=".$item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo $url."&page=".$pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo $url."&page=".$maxPage;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<div class="clear"></div>
									</td>
								</tr>
							</tfoot>
						</table>
					</div>
					<!-- End #tab1 -->


				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
